﻿#include "OperGraphTool.h"

#include "GraphDataProvider/GraphMarkProvider.h"
#include "GraphDataProvider/GraphProviderManager.h"

#include "FITK_Component/FITKAcousticsVTKGraphAdaptor/FITKAcousticsVTKGraphObject3D.h"
#include "FITK_Component/FITKAcousticsVTKGraphAdaptor/FITKAcousticsVTKCommons.h"
#include "FITK_Component/FITKRenderWindowVTK/FITKGraph3DWindowVTK.h"

Q_DECLARE_METATYPE(double*)

namespace GUIOper
{
    int OperGraphTool::createGraphTool(EventOper::GraphToolType type)
    {
        // 获取前处理可视化窗口。
        Comp::FITKGraph3DWindowVTK* graphWidget = getPreGraphWidget();
        if (graphWidget == nullptr) {
            return -1;
        }

        // 获取符号可视化对象管理器。
        GraphData::GraphMarkProvider* markProvider = GraphData::GraphProviderManager::getInstance()->getMarkProvider(graphWidget);
        if (markProvider == nullptr) {
            return -1;
        }

        Exchange::FITKAcousticsVTKGraphObject3D* graphObj = nullptr; 
        int dataObjId = -1;
        switch (type){
        case EventOper::GraphToolType::ToolDirection:
            graphObj = markProvider->getDirectionGraphObj(dataObjId);
            break;
        }
        return dataObjId;
    }

    void OperGraphTool::deleteToolObject(const int & toolId)
    {
        // 获取前处理可视化窗口。
        Comp::FITKGraph3DWindowVTK* graphWidget = getPreGraphWidget();
        if (graphWidget == nullptr) {
            return;
        }

        // 获取符号可视化对象管理器。
        GraphData::GraphMarkProvider* markProvider = GraphData::GraphProviderManager::getInstance()->getMarkProvider(graphWidget);
        if (markProvider == nullptr) {
            return;
        }

        markProvider->deleteToolGraphObj(toolId);

        // 刷新渲染。
        reRender();
    }

    void OperGraphTool::updateDirection(int objId, double * direction, double * pos)
    {
        // 获取前处理可视化窗口。
        Comp::FITKGraph3DWindowVTK* graphWidget = getPreGraphWidget();
        if (graphWidget == nullptr) {
            return;
        }

        // 获取符号可视化对象管理器。
        GraphData::GraphMarkProvider* markProvider = GraphData::GraphProviderManager::getInstance()->getMarkProvider(graphWidget);
        if (markProvider == nullptr) {
            return;
        }

        Exchange::FITKAcousticsVTKGraphObject3D* graphObj = markProvider->getDirectionGraphObj(objId);
        if (graphObj == nullptr) {
            return;
        }

        graphObj->setValue(Tool_KEY_DIRECTION_DIR, QVariant::fromValue(direction));
        graphObj->setValue(Tool_KEY_DIRECTION_POS, QVariant::fromValue(pos));
        graphObj->update();

        fastAddGraphObjectToWidget(graphObj, graphWidget);

        // 重新计算包围盒。
        graphWidget->updateBoundray();

        // 刷新渲染。
        reRender();
    }
}